package com.ruoyi.game.utils;

import cn.hutool.core.util.RandomUtil;
import com.github.binarywang.wxpay.bean.merchanttransfer.TransferCreateRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.ruoyi.common.utils.WxPayUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;

@Component
public class WxPayUser {
    //appid应用ID
    public static String APPID = "wx74f5d564d4c7967c";

    @Autowired
    private WxPayUtils wxPayUtils;

        public void payNow(String openId,Integer money,String remark){
        TransferCreateRequest transferCreateRequest = new TransferCreateRequest();
        transferCreateRequest.setAppid(APPID);
        //该笔批量转账的名称
        transferCreateRequest.setBatchName(remark);
        //转账说明，UTF8编码，最多允许32个字符
        transferCreateRequest.setBatchRemark(remark);
        //转账金额单位为“分”。转账总金额必须与批次内所有明细转账金额之和保持一致，否则无法发起转账操作
        transferCreateRequest.setTotalAmount(money);
        //一个转账批次单最多发起三千笔转账。转账总笔数必须与批次内所有明细之和保持一致，否则无法发起转账操作
        transferCreateRequest.setTotalNum(1);
        //商户系统内部的商家批次单号，要求此参数只能由数字、大小写字母组成，在商户系统内部唯一

        transferCreateRequest.setOutBatchNo(Md5Utils.hash(RandomUtil.randomString(10)));
        //发起批量转账的明细列表，最多三千笔
        ArrayList<TransferCreateRequest.TransferDetailList> transferDetailLists = new ArrayList<>();
        TransferCreateRequest.TransferDetailList transferDetailList = new TransferCreateRequest.TransferDetailList();
        //openid是微信用户在公众号appid下的唯一用户标识（appid不同，则获取到的openid就不同），可用于永久标记一个用户
        transferDetailList.setOpenid(openId);
        //“商家明细单号”
        transferDetailList.setOutDetailNo(Md5Utils.hash(RandomUtil.randomString(10)));
        //转账金额单位为分
        transferDetailList.setTransferAmount(money);
        //单条转账备注（微信用户会收到该备注），UTF8编码，最多允许32个字符
        transferDetailList.setTransferRemark(remark);
        transferDetailLists.add(transferDetailList);
        transferCreateRequest.setTransferDetailList(transferDetailLists);
        try {
            System.out.println(transferCreateRequest.toString());
            wxPayUtils.entPay(transferCreateRequest);
        } catch (WxPayException e) {
            System.out.println(e);
            throw new RuntimeException(e);
        }
    }



}
